﻿using System;
using System.Collections.Generic;
using System.Text;
using TarreOA.TarreOADAL;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOABLL
{
    public class SectionServices
    {

        private SectionDAO sectionDAO;

        public SectionServices()
        {
            sectionDAO = new SectionDAO();
        }

        //新增部门方法
        public Boolean createSection(string sectionName, string telephone,
            int baseSection, string facsimile, string remark, string address)
        {
            //int curPlace = sectionDAO.findPlaceByBaseSectionId(baseSection) + 1;
            return sectionDAO.createSection(sectionName, telephone, baseSection, facsimile, remark, address, 1);
        }

        //删除部门信息
        public Boolean delSection(int sectionId, int baseSectionId, int curPlace)
        {
            if (sectionDAO.delSectionInfo(sectionId) == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //上级部门信息
        public List<Section> findParentSectionInfo()
        {
            try
            {
                return sectionDAO.findAllSections();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return null;
        }

        //移动部门显示位置
        //public string changeSectionPlace(int baseSectionId, int curPlace, int SectionId, string action)
        //{
        //    try
        //    {
        //        if ("up".Equals(action))
        //        {
        //            if (curPlace == 1)
        //            {
        //                return "sizetop";
        //            }
        //            else
        //            {
        //                sectionDAO.changeSectionCurrentPlace(baseSectionId, SectionId, curPlace, action);
        //                return "move";
        //            }
        //        }
        //        else if ("down".Equals(action))
        //        {
        //            if (curPlace == sectionDAO.findPlaceByBaseSectionId(baseSectionId))
        //            {
        //                return "sizeout";
        //            }
        //            else
        //            {
        //                sectionDAO.changeSectionCurrentPlace(baseSectionId,SectionId, curPlace, action);
        //                return "move";
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.Message);
        //    }
        //    return null;
        //}

        //更新部门信息
        public int updateSectionInfo(string sectionId, string sectionName, string telephone,
            string baseSectionId, string facsimile, string remark, string address)
        {
            int sid = int.Parse(sectionId);
            int bsid = int.Parse(baseSectionId);
            return sectionDAO.updateSectionInfo(sid, sectionName, telephone, bsid, facsimile, remark, address);
        }

        //返回所有部门信息
        public List<Section> findAllSection()
        {
            return sectionDAO.findAllSections();
        }

        //根据上级部门编号查询部门信息
        public List<Section> findByBaseSectionId(int bsid)
        {
            return sectionDAO.findByBaseSection(bsid);
        }

        //递归查询所有下级部门信息
        public List<int> findAllChildSectionByBaseSectionId(int bsid)
        {
            List<Section> list = findByBaseSectionId(bsid);
            List<int> csids = new List<int>();
            csids.Add(bsid);
            if (list != null && list.Count > 0)
            {
                foreach (Section section in list)
                {
                    csids.Add(section.SectionId);
                    loopFindAllChildSection(csids, section.SectionId);
                }
            }
            return csids;
        }

        private void loopFindAllChildSection(List<int> csids, int bsid)
        {
            List<Section> clist = findByBaseSectionId(bsid);
            if (clist != null && clist.Count > 0)
            {
                foreach (Section section in clist)
                {
                    csids.Add(section.SectionId);
                    loopFindAllChildSection(csids, section.SectionId);
                }
            }
        }

        //根据部门ID查询子部门
        public static List<Section> findByBaseSection(int id)
        {
            SectionDAO sd = new SectionDAO();
            return sd.findByBaseSection(id);
        }
    }
}
